﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Dapper;

namespace Dapper.Library
{
    /// <summary>
    /// 指令
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class Command<T> : AbstractSet, ICommand<T>
    {
        protected Command(SqlProvider sqlProvider, IDbConnection dbCon, IDbTransaction dbTransaction) : base(dbCon, sqlProvider, dbTransaction)
        {
        }

        protected Command(SqlProvider sqlProvider, IDbConnection dbCon) : base(dbCon, sqlProvider)
        {
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(T entity)
        {
            int result = -1;
            SqlProvider.FormatUpdate(entity);
            result = DbCon.Execute(SqlProvider.SqlString, SqlProvider.Params, DbTransaction);

            SqlProvider.SetContext = new SetContext();
            SqlProvider.Params = new DynamicParameters();

            return result;
        }

        /// <summary>
        /// 异步更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(T entity)
        {
            int result = -1;
            SqlProvider.FormatUpdate(entity);
            result = await DbCon.ExecuteAsync(SqlProvider.SqlString, SqlProvider.Params, DbTransaction);

            SqlProvider.SetContext = new SetContext();
            SqlProvider.Params = new DynamicParameters();

            return result;
        }

        /// <summary>
        /// 带条件更新
        /// </summary>
        /// <param name="updateExpression"></param>
        /// <returns></returns>
        public int UpdateByExp(Expression<Func<T, T>> updateExpression)
        {
            int result = -1;
            SqlProvider.FormatUpdate(updateExpression);
            result = DbCon.Execute(SqlProvider.SqlString, SqlProvider.Params, DbTransaction);

            SqlProvider.SetContext = new SetContext();
            SqlProvider.Params = new DynamicParameters();

            return result;
        }

        /// <summary>
        /// 异步带条件更新
        /// </summary>
        /// <param name="updateExpression"></param>
        /// <returns></returns>
        public async Task<int> UpdateByExpAsync(Expression<Func<T, T>> updateExpression)
        {
            int result = -1;
            SqlProvider.FormatUpdate(updateExpression);
            result = await DbCon.ExecuteAsync(SqlProvider.SqlString, SqlProvider.Params, DbTransaction);

            SqlProvider.SetContext = new SetContext();
            SqlProvider.Params = new DynamicParameters();

            return result;
        }

        /// <summary>
        /// 删除功能
        /// </summary>
        /// <returns></returns>
        public int Delete()
        {
            int result = -1;
            SqlProvider.FormatDelete();
            result = DbCon.Execute(SqlProvider.SqlString, SqlProvider.Params, DbTransaction);

            SqlProvider.SetContext = new SetContext();
            SqlProvider.Params = new DynamicParameters();

            return result;
        }

        /// <summary>
        /// 异步删除
        /// </summary>
        /// <returns></returns>
        public async Task<int> DeleteAsync()
        {
            int result = -1;
            SqlProvider.FormatDelete();
            result = await DbCon.ExecuteAsync(SqlProvider.SqlString, SqlProvider.Params, DbTransaction);

            SqlProvider.SetContext = new SetContext();
            SqlProvider.Params = new DynamicParameters();

            return result;
        }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Insert(T entity)
        {
            int result = -1;
            SqlProvider.FormatInsert(entity);
            result =DbCon.Execute(SqlProvider.SqlString, SqlProvider.Params, DbTransaction);

            SqlProvider.SetContext = new SetContext();
            SqlProvider.Params = new DynamicParameters();

            return result;
        }

        /// <summary>
        /// 异步插入
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> InsertAsync(T entity)
        {
            int result = -1;
            SqlProvider.FormatInsert(entity);
            result = await DbCon.ExecuteAsync(SqlProvider.SqlString, SqlProvider.Params, DbTransaction);

            SqlProvider.SetContext = new SetContext();
            SqlProvider.Params = new DynamicParameters();

            return result;
        }

        /// <summary>
        /// 非查询功能
        /// </summary>
        /// <returns></returns>
        public int ExecuteNoQuery()
        {
            int result = -1;
            SqlProvider.FormatExecuteNoQuery();
            result = DbCon.Execute(SqlProvider.SqlString, SqlProvider.Params, DbTransaction);

            SqlProvider.SetContext = new SetContext();
            SqlProvider.Params = new DynamicParameters();

            return result;
        }
    }
}
